package com.fumin.common;

import com.fumin.model.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * 把request请求信息放到ThreadLocal里面,方便每个进程去取request的时候,取的是该线程对应的request
 * 高并发会非常有用
 * @Author xiayuxuanmin
 * @Date 2019/10/27
 * @Description
 */
public class RequestHolder {
    private static final ThreadLocal<SysUser> userHolder = new ThreadLocal<>();

    private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<>();

    /**
     * 添加元素
     * @param sysUser
     */
    public static void add(SysUser sysUser){
        userHolder.set(sysUser);
    }

    public static void add(HttpServletRequest request){
        requestHolder.set(request);
    }

    /**
     * 取出元素
     * @return
     */
    public static SysUser getCurrentUser(){
        return userHolder.get();
    }

    public static HttpServletRequest getCurrentRequest(){
        return requestHolder.get();
    }

    /**
     * 进程结束的时候,要移除操作;否则会一直保存在内存中,可能导致内存泄漏
     */
    public static void remove(){
        userHolder.remove();
        requestHolder.remove();
    }

}
